package org.lexicon.ontologydocumentor;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

class OntologyDocumentTransformer {
	static namespaces = [rdfs:"http://www.w3.org/2000/01/rdf-schema#",
		rdf:"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
		owl:"http://www.w3.org/2002/07/owl#",
		xsd:"http://www.w3.org/2001/XMLSchema#",
		dc:"http://purl.org/dc/elements/1.1/",
		dcterms:"http://purl.org/dc/terms/",
		foaf:"http://xmlns.com/foaf/0.1/"];
	
	static rdfProperties = [title: ResourceFactory.createProperty(namespaces.get("dc") + "title"),
							description: ResourceFactory.createProperty(namespaces.get("dc") + "description"),
							creator: ResourceFactory.createProperty(namespaces.get("dc") + "creator"),
							contributor: ResourceFactory.createProperty(namespaces.get("dc") + "contributor"),
							label: ResourceFactory.createProperty(namespaces.get("rdfs") + "label"),
							comment: ResourceFactory.createProperty(namespaces.get("rdfs") + "comment"),
							rights: ResourceFactory.createProperty(namespaces.get("dc") + "rights"),
							license: ResourceFactory.createProperty(namespaces.get("dcterms") + "license"),
							version: ResourceFactory.createProperty(namespaces.get("owl") + "versionInfo"),
							seeAlso: ResourceFactory.createProperty(namespaces.get("rdfs") + "seeAlso"),
							modified: ResourceFactory.createProperty(namespaces.get("dc") + "modified")]

	OntModel ontModel;
	Resource ontology;
	String htmlDocument;
	
	public OntologyDocumentTransformer(URI uri) {
		this(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM).read(uri.toString()));
	}
	
	public OntologyDocumentTransformer(OntModel o) {
		this.ontModel = o;
		this.ontModel.setNsPrefixes(OntologyDocumentTransformer.namespaces);
		Property p = this.ontModel.createProperty(namespaces.get("rdf") + "type");
		Resource r = this.ontModel.createResource(namespaces.get("owl") + "Ontology");
		ResIterator rs = this.ontModel.listResourcesWithProperty(p ,r);
		while (rs.hasNext()) {
			this.ontology = rs.next();
		}
	}
	
	public OntologyDocument transform() {
		OntologyDocument ontDoc = new OntologyDocument();
		ontDoc.setModel(this.ontModel);
		this.setTitle(ontDoc);
		this.setAuthors(ontDoc);
		this.setContributors(ontDoc);
		this.setThisVersion(ontDoc);
		this.setLastUpdate(ontDoc);
		this.setContactInformation(ontDoc);
		this.setLicense(ontDoc);
		this.setRights(ontDoc);
		this.setAcknowledgements(ontDoc);
		this.setClasses(ontDoc);
		this.setObjectProperties(ontDoc);
		this.setDatatypeProperties(ontDoc);
		this.setIndividuals(ontDoc);
		this.setDescription(ontDoc);
		return ontDoc;
	}
	
	public void setTitle(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("title"));
		while (rs.hasNext()) {
			doc.setTitle(rs.next().toString());
		}
	}
	
	public void setDescription(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("description"));
		while (rs.hasNext()) {
			String s = rs.next().toString();
			doc.setDescription(doc.getDescription() + " " + s);
		}
		
		NodeIterator rs2 = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("comment"));
		while (rs2.hasNext()) {
			String s = rs2.next().toString();
			doc.setDescription(doc.getDescription() + " " + s);
		}
	}
	
	public void setAuthors(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("creator"));
		while (rs.hasNext()) {
			doc.getAuthors().add(rs.next().toString());
		}
	}
	
	public void setContributors(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("contributor"));
		Integer counter = 0;
		while (rs.hasNext()) {
			doc.getContributors().add(rs.next().toString());
		}
	}
	
	public void setThisVersion(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("version"));
		while (rs.hasNext()) {
			doc.setThisVersion(rs.next().toString());
		}
	}
	
	
	public void setLastUpdate(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("modified"));
		while (rs.hasNext()) {
			doc.setLastUpdated(rs.next().toString());
		}
	}
	
	public void setContactInformation(OntologyDocument doc) {
	
	}
	
	public void setLicense(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("license"));
		while (rs.hasNext()) {
			doc.setLicense(rs.next().toString());
		}
		
	}
	
	public void setRights(OntologyDocument doc) {
		NodeIterator rs = this.ontModel.listObjectsOfProperty(this.ontology, rdfProperties.get("rights"));
		while (rs.hasNext()) {
			doc.setRights(rs.next().toString());
		}
		
	}
	
	public void setAcknowledgements(OntologyDocument doc) {
	
	}
	
	public void setClasses(OntologyDocument doc) {
		ExtendedIterator res = this.ontModel.listClasses();
		while (res.hasNext()) {
			doc.getClasses().add(res.next());
		}
		
	}
	
	public void setObjectProperties(OntologyDocument doc) {
		ExtendedIterator res = this.ontModel.listObjectProperties();
		while (res.hasNext()) {
			doc.getObjectProperties().add(res.next());
		}
	}
	
	public void setDatatypeProperties(OntologyDocument doc) {
		ExtendedIterator res = this.ontModel.listDatatypeProperties();
		while (res.hasNext()) {
			doc.getDatatypeProperties().add(res.next());
		}
	}
	
	public void setIndividuals(OntologyDocument doc) {
		ExtendedIterator res = this.ontModel.listIndividuals();
		while (res.hasNext()) {
			doc.getIndividuals().add(res.next());
		}
	}
	
}
